/**
 * 页面切换时的动画效果
 * ibio-develop
 * 2008-11-20 16:17
 */
package com.ibio8.transitions {
	import com.ibio8.utils.MathUtil;
	
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.display.MovieClip;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	
	public class PageStackEffect {
		public static const TOTAL_NUM:uint = 10;
		public static const RANDOM:int = -2;
		
		protected var m_transition:TransitionManager;
		protected var m_duration:Number;
		protected var m_doLater:Function;
		
		/**
		 * 构造函数
		 * @param	doLater		执行完动画后的回调函数
		 */
		public function PageStackEffect(doLater:Function = null) {
			m_doLater = doLater;
		}
		
		/**
		 * 设置要过渡的 MC
		 * @param	target		需要过渡的MC
		 * @param	duration	过渡的秒数
		 * @param	effect		过渡的效果
		 * 具体参数为
		 * 0 -- TOTAL_NUM、RANDOM、若不填则需要调用具体的效果函数启动效果
		 */
		public function targetPage(target:MovieClip, duration:Number = 1, effect:int = -1):void {
			m_transition = new TransitionManager(target);
			m_duration = duration;
			if (effect == -1) {
				return;
			}else {
				var num:int = effect;
				if (effect == RANDOM) {
					num = MathUtil.random(0, TOTAL_NUM);
				}
				switch(num) {
					case 0:
						blinds();
						break;
					case 1:
						fade();
						break;
					case 2:
						fly();
						break;
					case 3:
						iris();
						break;
					case 4:
						photo();
						break;
					case 5:
						pixelDissolve();
						break;
					case 6:
						rotate();
						break;
					case 7:
						squeeze();
						break;
					case 8:
						wipe();
						break;
					case 9:
						zoom();
						break;
					default:
						blinds();
						break;
				}
			}
		}
		
		/**
		 * 遮帘过渡
		 * @param	dimension	遮罩条纹是垂直的 (0) 还是水平的 (1)
		 * @param	numStrips	“遮帘”效果中的遮罩条纹数。 建议的范围是 1 到 50
		 */
		public function blinds(dimension:uint = 0, numStrips:uint = 10):void{
			m_transition.startTransition({type:Blinds, direction:Transition.IN, duration:m_duration, easing:None.easeNone, numStrips:numStrips, dimension:dimension});
			setTimer();
		}
		
		//淡化过渡
		public function fade():void{
			m_transition.startTransition({type:Fade, direction:Transition.IN, duration:m_duration, easing:None.easeNone});
			setTimer();
		}
		
		/**
		 * 飞行过渡
		 * @param	startPoint	一个指示起始位置的整数；
		 * 左上 1；上中 2；右上 3；左中 4；中心 5；右中 6；左下 7；下中 8；右下 9
		 */
		public function fly(startPoint:uint = 1):void{
			m_transition.startTransition({type:Fly, direction:Transition.IN, duration:m_duration, easing:Elastic.easeOut, startPoint:startPoint});
			setTimer();
		}
		
		/**
		 * 光圈过渡
		 * @param	shape		遮罩形状 方形(0)或 圆形(1)
		 * @param	startPoint	一个指示起始位置的整数；
		 * 左上 1；上中 2；右上 3；左中 4；中心 5；右中 6；左下 7；下中 8；右下 9
		 */
		public function iris(shape:uint = 0, startPoint:uint = 5):void {
			var flag:String;
			if (shape == 0) {
				flag = Iris.SQUARE;
			}else {
				flag = Iris.CIRCLE;
			}
			m_transition.startTransition({type:Iris, direction:Transition.IN, duration:m_duration, easing:Strong.easeOut, startPoint:startPoint, shape:flag});
			setTimer();
		}
		
		//照片过渡
		public function photo():void{
			m_transition.startTransition({type:Photo, direction:Transition.IN, duration:m_duration, easing:None.easeNone});
			setTimer();
		}
		
		/**
		 * 像素溶解
		 * @param	xSections	沿水平轴的遮罩矩形部分的数目。 建议的范围是 1 到 50
		 * @param	ySections	沿垂直轴的遮罩矩形部分的数目。 建议的范围是 1 到 50
		 */
		public function pixelDissolve(xSections:uint = 10, ySections:uint = 10):void{
			m_transition.startTransition({type:PixelDissolve, direction:Transition.IN, duration:m_duration, easing:None.easeNone, xSections:xSections, ySections:ySections});
			setTimer();
		}
		
		/**
		 * 旋转
		 * @param	ccw		对于顺时针旋转为 false；对于逆时针旋转为 true
		 * @param	degrees	旋转的度数。 建议的范围是 1 到 9999
		 */
		function rotate(ccw:Boolean = false, degrees:uint = 720):void{
			m_transition.startTransition({type:Rotate, direction:Transition.IN, duration:m_duration, easing:Strong.easeInOut, ccw:ccw, degrees:degrees});
			setTimer();
		}
		
		/**
		 * 挤压过渡
		 * @param	dimension 指示“挤压”效果应是水平的 (0) 还是垂直的 (1)
		 */
		public function squeeze(dimension:uint = 1):void{
			m_transition.startTransition({type:Squeeze, direction:Transition.IN, duration:m_duration, easing:Elastic.easeOut, dimension:dimension});
			setTimer();
		}
		
		/**
		 * 划入划出
		 * @param	startPoint 一个指示起始位置的整数；
		 * 左上 1；上中 2；右上 3；左中 4；中心 5；右中 6；左下 7；下中 8；右下 9
		 */
		public function wipe(startPoint:uint = 1):void{
			m_transition.startTransition({type:Wipe, direction:Transition.IN, duration:m_duration, easing:None.easeNone, startPoint:startPoint});
			setTimer();
		}
		
		//缩放
		public function zoom():void{
			m_transition.startTransition({type:Zoom, direction:Transition.IN, duration:m_duration, easing:Elastic.easeOut});
			setTimer();
		}
		
		protected function setTimer():void {
			if (m_doLater != null) {
				var timer:Timer = new Timer(m_duration * 1000, 1);
				timer.addEventListener(TimerEvent.TIMER, onCompleteHandler);
				timer.start();
			}
		}
		
		protected function onCompleteHandler(e:TimerEvent):void {
			e.currentTarget.removeEventListener(TimerEvent.TIMER, onCompleteHandler);
			m_doLater();
		}
	}	
}

